#ifndef __CGI_CONTROL_H__
#define __CGI_CONTROL_H__

#include <map>
#include <string>
#include <sys/types.h>
#include <regex.h>
#include <vector>
#include <list>

#include <global.h>
#include <noncopyable.h>
#include <MarkupSTL.h>
#include <log.h>
#include <keydic.h>

HTTP_SVR_NS_BEGIN

class THttpStateInfo;

typedef struct tagCgiInfo
{
    char            _szUnixPath[MAX_PATH_LEN];
	char            _szCgiName[MAX_PATH_LEN];
    char            _szSvrPath[MAX_PATH_LEN];
    int             _nPathLen;
    int             _nIndex;
    pid_t           _nPid;
    char            location[MAX_PATH_LEN];
    char*            static500;
    int               static500_len;
    char            mime[128];
    int               gzip;
    int             _bStatic;
    bool            _gzip_enable;
    int             _gzip_inf;
    std::vector<std::string> _vHostList;

}TCGIInfo;

class TCGIControl : private noncopyable
{
    typedef CKeydic<TCGIInfo> TCGIInfoDic;

public:
    TCGIControl (void);
    ~TCGIControl (void);

    static TCGIControl* Instance (void);
    static void Destroy(void);

public:

    int open (const char* xmlfile);

    static inline TCGIInfo* get_cgi_info (void)
    {
        return _stCgiList;
    }
    
    static inline int get_cgi_count (void)
    {
        return _nCgiCount;
    }

    inline TCGIInfo* get_cgi_switch (void)
    {
        return _pCgiSwitch;
    }

    TCGIInfo* find_cgi (const char* cgi_path, int len);
    
    TCGIInfo* find_cgi_ext(const char* url, int len);

	//virtual_host<==>cginame
    std::map<std::string, std::string>  _mDefault_cginame;

protected:
	TCGIInfo* prefix_mach(const char* url, int len);
    
private: //property
    static int                  _nCgiCount;
    static TCGIInfo*            _stCgiList;

    TCGIInfoDic*                _stCgiDics;
    TCGIInfo*                   _pCgiSwitch;    
    std::map<std::string, int>  _mSwitchList;
    std::map<std::string, std::string>  _mSwitchList_cginame;
    int  _support_prefix;
};

HTTP_SVR_NS_END

#endif //__CGI_CONTROL_H__

